<?php if(!defined('DEBUG')) die;

class ImageResizerController extends Controller
{
	public function index()
	{
		$args = func_get_args();
		$file_path = join('/', $args);
		
		$ext = preg_replace( '/^.+\./', '', $file_path );
		
		if( $ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png' )
			page_not_found();
			
		$f_expl = explode('-', $args[count($args)-1] );
		$f_size = explode('x', array_shift($f_expl));
		$f_name = join('-', $f_expl);
		
		array_pop($args);
		
		$real_file_path = realpath(FROG_ROOT .'/'. PUBLIC_FILES .'/') . join('/', $args) .'/'. $f_name;		
		
		if( count($f_size) < 2 || file_exists( $real_file_path ) == false )
			page_not_found();
		
		use_helper('Dir');
		
		$image = new DirFileImage( $real_file_path );
		$image->resize( $f_size[0], $f_size[1] );
		
		echo($image);
	}
}

Dispatcher::addRoute(array(
	'/' . PUBLIC_FILES . '/:any' => 'image_resizer/index/$1'
));

?>